package com.qf.business.user.core.controller;

import com.qf.business.user.core.service.SysDepartmentService;
import com.qf.commons.core.utils.QfBeanUtils;
import com.qf.commons.data.result.R;
import com.qf.data.user.entity.SysDepartment;
import com.qf.data.user.vo.input.SysDepartmentInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 部门管理的Controller
 * @author YaoJiXing
 * created 2022/9/9 18:05
 */
@RestController
@RequestMapping("/sys/dep")
@Slf4j
public class DepController {

    @Autowired
    private SysDepartmentService sysDepartmentService;

    /**
     * 查询部门列表
     * @return 返回部门列表信息
     */
    @RequestMapping("/list")
    public R<List<SysDepartment>> list() {
        List<SysDepartment> deps = sysDepartmentService.list();
        return R.create(deps);
    }

    @RequestMapping("/insert")
    public R insert(@Valid SysDepartmentInput sysDepartmentInput) {
        log.debug("[dep insert] 部门新增 - {}", sysDepartmentInput);
        //类型转换 vo -> entity
        SysDepartment sysDepartment = QfBeanUtils.copyBean(sysDepartmentInput, SysDepartment.class);
        sysDepartment.setId((int) (Math.random() * 100000));
        //调用业务层进行保存
        sysDepartmentService.save(sysDepartment);

        return R.create("success");
    }

}
